package com.ihmp.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ihmp.model.TProductInRoomPriceHistory;

@Repository("productInRoomPriceHistoryDAO")
public class CProductInRoomPriceHistoryDAO implements IProductInRoomPriceHistoryDAO {
	@Autowired
	private SessionFactory sessionFactory;

	public void saveOrUpdate(TProductInRoomPriceHistory productPriceHistory) {		
		sessionFactory.getCurrentSession().saveOrUpdate(productPriceHistory);
	}
	
	public void delete(TProductInRoomPriceHistory productPriceHistory) {		
		sessionFactory.getCurrentSession().delete(productPriceHistory);
	}

	public TProductInRoomPriceHistory get(Integer id) {		
		return (TProductInRoomPriceHistory) sessionFactory.getCurrentSession().get(TProductInRoomPriceHistory.class, id);
		
	}

	@SuppressWarnings("unchecked")
	public List<TProductInRoomPriceHistory> list() {		
		Criteria crit = sessionFactory.getCurrentSession().createCriteria(TProductInRoomPriceHistory.class);
		crit.addOrder(Order.desc("createDate"));
		crit.addOrder(Order.desc("editDate"));
		return crit.list();
	}

	@SuppressWarnings("unchecked")
	public List<TProductInRoomPriceHistory> list(int page,int recordPerPage) {		
		Criteria crit = sessionFactory.getCurrentSession().createCriteria(TProductInRoomPriceHistory.class);
		crit.setFirstResult((page-1)*recordPerPage).setMaxResults(page*recordPerPage -1);
		crit.addOrder(Order.desc("createDate"));
		crit.addOrder(Order.desc("editDate"));
		return crit.list();
	}
}
